package com.myself.ssoserver.entity

import com.fasterxml.jackson.annotation.JsonIgnore
import com.myself.common.entity.BaseEntity
import com.myself.common.table.BaseTable
import org.ktorm.database.Database
import org.ktorm.entity.Entity
import org.ktorm.entity.sequenceOf
import org.ktorm.schema.int
import org.ktorm.schema.varchar

// ------------------- Entity & Table 扩展 -------------------
interface User : BaseEntity<User> {
    var username: String
    @get:JsonIgnore
    var password: String
    @get:JsonIgnore
    var lastLoginIp: String?
    var email: String
    var isActive : Int
    var role : Role?
    companion object : Entity.Factory<User>()
}

object Users : BaseTable<User>("sys_user") {
    val isActive = int("is_active").bindTo { it.isActive }
    val username = varchar("username").bindTo { it.username }
    val password = varchar("password").bindTo { it.password }
    val lastLoginIp = varchar("last_login_ip").bindTo { it.lastLoginIp }
    val email = varchar("email").bindTo { it.email }
    val roleId = int("role_id").references(Roles) { it.role }
    val Database.users get() = this.sequenceOf(Users)
}


// LoginHistory.kt
interface LoginHistory : BaseEntity<LoginHistory> {
    var userId: Int
    var loginIp: String
    var userAgent: String?
    companion object : Entity.Factory<LoginHistory>()
}

object LoginHistories : BaseTable<LoginHistory>("sys_login_history") {
    val userId = int("user_id").bindTo { it.userId }
    val loginIp = varchar("login_ip").bindTo { it.loginIp }
    val userAgent = varchar("user_agent").bindTo { it.userAgent }
    val Database.loginHistories get() = this.sequenceOf(LoginHistories)
}